package com.xunge.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xunge.mapper.HomeworkSubmissionMapper;
import com.xunge.pojo.HomeworkSubmission;
import com.xunge.pojo.HomeworkSubmissionQueryParam;
import com.xunge.pojo.PageResult;
import com.xunge.service.HomeworkSubmissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class HomeworkSubmissionServiceImpl implements HomeworkSubmissionService {

    @Autowired
    private HomeworkSubmissionMapper homeworkSubmissionMapper;

    /**
     * 分页查询作业提交信息
     */
    @Override
    public PageResult<HomeworkSubmission> page(HomeworkSubmissionQueryParam homeworkSubmissionQueryParam) {
        // 设置分页参数
        PageHelper.startPage(homeworkSubmissionQueryParam.getPage(), homeworkSubmissionQueryParam.getPageSize());
        // 执行查询
        Page<HomeworkSubmission> page = (Page<HomeworkSubmission>) homeworkSubmissionMapper.list(homeworkSubmissionQueryParam);
        // 封装分页结果
        return new PageResult<>(page.getTotal(), page.getResult());
    }

    /**
     * 新增作业提交
     */
    @Override
    public void save(HomeworkSubmission homeworkSubmission) {
        homeworkSubmission.setCreateTime(LocalDateTime.now());
        homeworkSubmission.setUpdateTime(LocalDateTime.now());
        homeworkSubmissionMapper.insert(homeworkSubmission);
    }

    /**
     * 根据ID查询作业提交
     */
    @Override
    public HomeworkSubmission getById(Integer id) {
        return homeworkSubmissionMapper.getById(id);
    }

    /**
     * 根据ID修改作业提交
     */
    @Override
    public void update(HomeworkSubmission homeworkSubmission) {
        homeworkSubmission.setUpdateTime(LocalDateTime.now());
        homeworkSubmissionMapper.update(homeworkSubmission);
    }

    /**
     * 根据ID删除作业提交
     */
    @Override
    public void deleteById(Integer id) {
        homeworkSubmissionMapper.deleteById(id);
    }

    /**
     * 批量删除作业提交
     */
    @Override
    public void delete(List<Integer> ids) {
        homeworkSubmissionMapper.deleteByIds(ids);
    }

    /**
     * 根据作业ID查询提交记录
     */
    @Override
    public List<HomeworkSubmission> getByHomeworkId(Integer homeworkId) {
        return homeworkSubmissionMapper.getByHomeworkId(homeworkId);
    }

    /**
     * 根据学生ID查询提交记录
     */
    @Override
    public List<HomeworkSubmission> getByStudentId(Integer studentId) {
        return homeworkSubmissionMapper.getByStudentId(studentId);
    }

    /**
     * 根据作业ID和学生ID查询提交记录
     */
    @Override
    public HomeworkSubmission getByHomeworkIdAndStudentId(Integer homeworkId, Integer studentId) {
        return homeworkSubmissionMapper.getByHomeworkIdAndStudentId(homeworkId, studentId);
    }
} 